//
//  CSAddressViewController.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2018/1/2.
//

#import "CSAddressViewController.h"
#import "CSPlaceModel.h"

@interface CSAddressViewController ()

@property (nonatomic, strong) CS_Page_TextFieldView *locality_CS_Page_TextFieldView;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) CS_Page_Button *save_CS_Page_Button;
@property (nonatomic, strong) CS_Page_TextFieldView *detail_CS_Page_TextFieldView;

@end

@implementation CSAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"地址设置";
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat primaryY = 0;
    primaryY = [self.locality_CS_Page_TextFieldView layoutView:self.view primaryY:primaryY];
    primaryY = [self.detail_CS_Page_TextFieldView layoutView:self.view primaryY:primaryY];
    [self.save_CS_Page_Button layoutView:self.view primaryY:primaryY];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(CS_Page_TextFieldView *)locality_CS_Page_TextFieldView{
    
    if (_locality_CS_Page_TextFieldView == nil) {
        
        WS(weakSelf);
        NSString *addressString = @"";
        if (self.currentGpsAddress) {
            addressString = [NSString stringWithFormat:@"%@ %@ %@", self.currentGpsAddress.administrativeArea, self.currentGpsAddress.locality, self.currentGpsAddress.subLocality];
        }
        _locality_CS_Page_TextFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:@"所在区域" keyboardType:UIKeyboardTypePhonePad data:addressString];
        _locality_CS_Page_TextFieldView.textFieldType = CSTextFieldType_Click;
        _locality_CS_Page_TextFieldView.clickHandler = ^(CS_Page_TextFieldView *page_TextFieldView) {
            
            if (weakSelf.placeList) {
                
                CSLocalityViewController *localityViewController = [CSLocalityViewController new];
                localityViewController.placeList = weakSelf.placeList;
                localityViewController.navigationController.title = @"选择城市";
                localityViewController.currentGpsAddress = weakSelf.currentGpsAddress;
                localityViewController.onLocationSelectedHandler = ^(NSString *localString) {
                    weakSelf.locality_CS_Page_TextFieldView.value = localString;
                };
                [weakSelf.navigationController pushViewController:localityViewController animated:YES];
                
            }
            
        };
        
    }
    return _locality_CS_Page_TextFieldView;
    
}

-(UITextView *)textView{
    
    if (_textView == nil) {
        _textView = [UITextView new];
        _textView.textColor = COLOR_TEXT_1;
        _textView.font = [UIFont systemFontOfSize:FONT_SIZE_1];
        _textView.layer.borderColor = COLOR_TOP_SEPERATOR_LINE.CGColor;
        
        _textView.layer.borderWidth = 1.0;
    }
    return _textView;
    
}

-(CS_Page_Button *)save_CS_Page_Button{
    
    if (_save_CS_Page_Button == nil) {
        
        WS(weakSelf);
        _save_CS_Page_Button = [CS_Page_Button new];
        _save_CS_Page_Button.buttonName = @"确定";
        _save_CS_Page_Button.clickHandler = ^{
            
            if (weakSelf.onLocationEditedHandler) {
                
                NSString *locationString = [NSString stringWithFormat:@"%@%@", [weakSelf.locality_CS_Page_TextFieldView.value replaceCharcter:@" " withCharcter:@""], weakSelf.detail_CS_Page_TextFieldView.value];
                weakSelf.onLocationEditedHandler(locationString);
                
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        };
        
    }
    return _save_CS_Page_Button;
    
}

-(CS_Page_TextFieldView *)detail_CS_Page_TextFieldView{
    
    if (_detail_CS_Page_TextFieldView == nil) {
        _detail_CS_Page_TextFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:@"详细地址" keyboardType:UIKeyboardTypeDefault data:@""];
    }
    return _detail_CS_Page_TextFieldView;
    
}

@end
